package com.itcast.springboot.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.itcast.springboot.bean.SongList;
import com.itcast.springboot.utils.FileUploadUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.*;

/**
 * w
 * 图片上传功能
 */
@RestController
@CrossOrigin
public class Test {
    @PostMapping(value = "/uploadPhoto")
    public String uploadPhoto(MultipartFile photo){
        //上传图片
        if (photo == null){
            return "图片不存在";
        }
        String serverPath = FileUploadUtils.fileUpload(1, photo);
        if (serverPath == null){
            return "上传失败";
        }
        return serverPath;
    }
    @PostMapping(value = "/uploadHeadPhoto")
    public String uploadHeadPhoto(HttpServletRequest request) throws IOException {
        String stringSongList = request.getParameter("songList");
        JSONObject jsonSongList = JSON.parseObject(stringSongList);//将json字符串转换为json对
        SongList songList = JSON.parseObject(stringSongList, SongList.class);
        //将string转为json对象
        System.out.println(jsonSongList);
        //将json字符串转为javaBean
        System.out.println(songList);
        MultipartFile headPhotoFile = null;
        //取出form-data中的二进制字段
        MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
        headPhotoFile = multipartRequest.getFile("file");//file是form-data中二进制字段对应的name

        //文件未上传
        if (headPhotoFile.isEmpty()) {
            return "文件未上传";
        } else {
            //获取原始图片名称
            String photoName = headPhotoFile.getOriginalFilename();
            //新的图片名字
            String newPhotoName = UUID.randomUUID() + photoName;
            System.out.println("新的图片名称： " + newPhotoName);
            //图片在项目中的全路径（绝对路径）
            String photoFullPath = System.getProperty("user.dir").concat("\\src\\main\\resources\\" +
                    "static\\images\\" + newPhotoName);
            String filePath = "D://images//";
            System.out.println("在项目中的绝对路径： " + photoFullPath);
            //图片想要上传的本地路径位置
            String photoLocalPath = filePath.concat(newPhotoName);
            //在内存中创建File文件映射对象
            File dest = new File(photoFullPath);
            //父目录是否存在，不存在则创建
            if (!dest.getParentFile().exists()) {
                dest.getParentFile().mkdirs();
            }
            try {
                //创建文件
                headPhotoFile.transferTo(dest);
            } catch (IOException e) {
                e.printStackTrace();
            }
            return "文件上传成功";
        }
    }

    //图片删除功能
    @DeleteMapping("/photo")
    public String deletePhoto() {
        String filePath = "D://images//";
        String photoName = "index.jpg";
        String photoLocalPath = filePath.concat(photoName);
        File file = new File(photoLocalPath);
        String resultInfo = null;
        if (file.exists()) {
            if (file.delete()) {
                resultInfo = "1-删除成功";
            } else {
                resultInfo = "0-删除失败";
            }
        } else {
            resultInfo = "文件不存在！";
        }
        return resultInfo;
    }
}
